home *** CD-ROM | disk | FTP | other *** search
- Path: rover.ucs.ualberta.ca!news
- From: ryangall@gpu.srv.ualberta.ca (Bobby Sixkiller)
- Newsgroups: comp.lang.c
- Subject: allocating unlimited string input....HELP
- Date: 10 Jan 1996 07:23:18 GMT
- Organization: University of Alberta, Edmonton, Canada
- Message-ID: <4cvph6$s8s@pulp.ucs.ualberta.ca>
- NNTP-Posting-Host: async4-15.remote.ualberta.ca
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.99.2
-
- I am doing an assignment for my computing class, and seem to have run into
- a small problem. I am supposed to be able to read an unlimited string from
- stdin, without defining maximum size. I thought that this problem would be
- simple....but I have been at it for a while now. Ill show you what my last
- resort was.....
-
-
-
- int read(char *S)
- {
- char *A,*B;
- char ch,count=0;
-
- A=NULL;
- B=NULL;
-
- ch=getc(stdin);
-
- while(ch!='\n')
- {
- B=(char *) malloc(count+2);
- if(count>0)
- {
- sprintf(B,"%s%c",A,ch);
- free(A);
- }
- else sprintf(B,"%c",ch);
-
- printf("%s---%i----%i\n",B,strlen(B),count);
- A=B;
- ch=getc(stdin);
- count=count+1;
- }
-
- }
-
-
- I know its ugly, but its the third version....and I tried all the other
- possibilities that I know of that would have better style. Anyways, they
- all pretty much are doing the same thing...oh yeah, about char *S, thats
- for later, Im still stuck trying to get the bulk of the function to work,
- once its working, I will assign A to S........
-
- Anyways, the function reads in just fine, but for some reason when it gets
- to 146 characters it bails out, loses char *B and starts reducing
- count?!&*#$@ what the hell?! I know I can uses some data structure, or
- just a linked list to solve this, but this assignment limits you to one
- module, and no header files....and I dont really want to start haveing
- 2000 lines of code for such a simple problem?! I guess they think its
- simple enough.....so did I?!
-
-
- please mail me ASAP....thanks!
-
-